﻿using System;
using System.IO;
using System.Linq;
using Abp.Reflection.Extensions;

namespace FwProject.Web
{
    /// <summary>
    /// This class is used to find root path of the web project in;
    /// unit tests (to find views) and entity framework core command line commands (to find conn string).
    /// </summary>
    public static class WebContentDirectoryFinder
    {
        public static string CalculateContentRootFolder()
        {
            var coreAssemblyDirectoryPath = Path.GetDirectoryName(typeof(FwProjectCoreModule).GetAssembly().Location);
         

            if (coreAssemblyDirectoryPath == null)
            {
                throw new Exception("Could not find location of FwProject.Core assembly!");
            }

            var directoryInfo = new DirectoryInfo(coreAssemblyDirectoryPath);

            while (!DirectoryContains(directoryInfo.FullName, "appsettings.json"))
                    {
                
                        if (directoryInfo.Parent == null)
                        {
                            throw new Exception("Could not find content root folder!");
                        }

                        directoryInfo = directoryInfo.Parent;
                  }

                var webMvcFolder = Path.Combine(directoryInfo.FullName);
                if (Directory.Exists(webMvcFolder))
                {
                    return webMvcFolder;
                }

            throw new Exception("Could not find root folder of the web project!");
        }

        private static bool DirectoryContains(string directory, string fileName)
        {
            bool flag = true;
            flag= Directory.GetFiles(directory).Any(filePath => string.Equals(Path.GetFileName(filePath), fileName));
            return flag;
        }
    }
}
